import Toast from "@vant/weapp/toast/toast";

// pages/goods/goods.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    height: 0,
    top: 0,
    isSticky: false,
    addCartMask: false,
    active: 0,
    goodsList1: [
      {
        id: 1,
        title: "商品1商品1商品1商品1商品1商品1商品1",
        detail:
          "详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情",
        price: 99.99,
        img: "/static/images/logo.png",
      },
      {
        id: 2,
        title: "商品2",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/logo1.png",
      },
      {
        id: 3,
        title: "商品3",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/1.png",
      },
      {
        id: 4,
        title: "商品4",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/share.png",
      },
      {
        id: 5,
        title: "商品5",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/2.png",
      },
      {
        id: 6,
        title: "商品6",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/2.png",
      },
      {
        id: 7,
        title: "商品7",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "https://i.imgtg.com/2023/06/29/OkJIel.png",
      },
      {
        id: 8,
        title: "商品8",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "https://i.imgtg.com/2023/06/29/OkJ3Gg.png",
      },
    ],
    goodsList2: [
      {
        id: 5,
        title: "商品5",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/2.png",
      },
      {
        id: 6,
        title: "商品6",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/2.png",
      },
      {
        id: 7,
        title: "商品7",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "https://i.imgtg.com/2023/06/29/OkJ3Gg.png",
      },
      {
        id: 8,
        title: "商品8",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "https://i.imgtg.com/2023/06/29/OkJ3Gg.png",
      },
      {
        id: 1,
        title: "商品1商品1商品1商品1商品1商品1商品1",
        detail:
          "详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情",
        price: 99.99,
        img: "/static/images/logo.png",
      },
      {
        id: 2,
        title: "商品2",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/logo1.png",
      },
      {
        id: 3,
        title: "商品3",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/1.png",
      },
      {
        id: 4,
        title: "商品4",
        detail: "商品2详情商品2详情商品2详情商品2详情",
        price: 99.99,
        img: "/static/images/share.png",
      },
    ],
  },

  // 搜索框
  onChange(e) {
    this.setData({
      value: e.detail,
    });
  },

  onSearch() {
    Toast("搜索：" + this.data.value);
  },

  onClick() {
    Toast("搜索：" + this.data.value);
  },

  onCancel() {
    Toast("取消：" + this.data.value);
  },

  // 轮播图自适应图片高度
  height: function () {
    let that = this;
    var query = wx.createSelectorQuery();
    query
      .select(".swiper-img")
      .boundingClientRect(function (rect) {
        that.setData({
          // 获取要循环标签的高度
          height: rect.height,
        });
        // console.log(that.data.height);
      })
      .exec();
  },

  jumpnubiao() {
    wx.pageScrollTo({
      selector: "#main",
    });
  },

  onPageScroll(e) {
    const { top } = this.data;
    const scrollTop = e.scrollTop;
    if (scrollTop >= top) {
      this.setData({ isSticky: true });
    }
    // console.log(top);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.height();
    var obj = wx.createSelectorQuery();
    obj
      .selectAll("#main")
      .boundingClientRect(function (rect) {
        // console.log(rect[0].top);

        that.setData({
          top: rect[0].top,
        });
      })
      .exec();
  },

  goToDetail() {
    wx.navigateTo({
      url: "/pages/detail/detail",
    });
  },

  showAddCartMask() {
    const that = this;
    wx.navigateTo({
      url: "/pages/detail/detail?showmask=true",
      success: (result) => {
        that.setData({ showMask: true });
      },
      fail: () => {},
      complete: () => {},
    });
  },

  // hideAddCartMask() {
  //   console.log(11);
  //   this.setData({ addCartMask: false });
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1,
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  // 分享到朋友圈
  onShareTimeline() {},
});
